﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.JointVariableLimitsEditor.CUiJVLEditorColorDialog
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace DnProcessSimulateCommands.JointVariableLimitsEditor;

internal class CUiJVLEditorColorDialog : ColorDialog
{
  private const int WM_INITDIALOG = 272;
  private const int WM_DESTROY = 2;
  private const uint SWP_NOSIZE = 1;
  private const uint SWP_SHOWWINDOW = 64 /*0x40*/;
  private const uint SWP_NOZORDER = 4;
  private const uint UFLAGS = 69;
  private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
  private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
  private static readonly IntPtr HWND_TOP = new IntPtr(0);
  private static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
  private string _title;
  private Point? _pos;

  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  private static extern bool SetWindowText(IntPtr hWnd, string text);

  [DllImport("user32.dll", SetLastError = true)]
  [return: MarshalAs(UnmanagedType.Bool)]
  private static extern bool SetWindowPos(
    IntPtr hWnd,
    IntPtr hWndInsertAfter,
    int x,
    int y,
    int cx,
    int cy,
    uint uFlags);

  [DllImport("user32.dll")]
  [return: MarshalAs(UnmanagedType.Bool)]
  private static extern bool GetWindowRect(IntPtr hWnd, out CUiJVLEditorColorDialog.RECT lpRect);

  public CUiJVLEditorColorDialog()
  {
    this._title = (string) null;
    this._pos = new Point?();
  }

  public CUiJVLEditorColorDialog(string text, Point? pos)
  {
    this._title = text;
    this._pos = pos;
  }

  internal Point? Pos
  {
    get => this._pos;
    set => this._pos = value;
  }

  internal string Title
  {
    get => this._title;
    set => this._title = value;
  }

  protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
  {
    if (msg == 2)
    {
      try
      {
        CUiJVLEditorColorDialog.RECT lpRect;
        if (CUiJVLEditorColorDialog.GetWindowRect(hWnd, out lpRect))
          this._pos = new Point?(new Point(lpRect.Left, lpRect.Top));
      }
      catch
      {
      }
    }
    IntPtr num = base.HookProc(hWnd, msg, wparam, lparam);
    if (msg == 272)
    {
      if (!string.IsNullOrEmpty(this._title))
      {
        try
        {
          CUiJVLEditorColorDialog.SetWindowText(hWnd, this._title);
        }
        catch
        {
        }
      }
      if (this._pos.HasValue)
      {
        try
        {
          IntPtr hWnd1 = hWnd;
          IntPtr hwndTop = CUiJVLEditorColorDialog.HWND_TOP;
          Point point = this._pos.Value;
          int x = point.X;
          point = this._pos.Value;
          int y = point.Y;
          CUiJVLEditorColorDialog.SetWindowPos(hWnd1, hwndTop, x, y, 0, 0, 69U);
        }
        catch
        {
        }
      }
    }
    return num;
  }

  public struct RECT
  {
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
  }
}
